package com.bocai.eelly.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.activitys.ChoosePhotosActivity;
import com.bocai.eelly.common.BaseFragment;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.net.Url;
import com.bocai.eelly.utils.BitmapUtils;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.OkHttpClientManager;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.squareup.okhttp.Request;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PostFuliaoStockFragment extends BaseFragment implements View.OnClickListener{
    @Bind(R.id.et_post_fuliao_stock_name)
    EditText etPostFuliaoStockName;
    @Bind(R.id.et_post_fuliao_stock_model)
    EditText etPostFuliaoStockModel;
    @Bind(R.id.et_post_fuliao_stock_format)
    EditText etPostFuliaoStockFormat;
    @Bind(R.id.et_post_fuliao_stock_brand)
    EditText etPostFuliaoStockBrand;
    @Bind(R.id.et_post_fuliao_stock_color)
    EditText etPostFuliaoStockColor;
    @Bind(R.id.et_post_fuliao_stock_num)
    EditText etPostFuliaoStockNum;
    @Bind(R.id.et_post_fuliao_stock_backup)
    EditText etPostFuliaoStockBackup;
    @Bind(R.id.ll_post_fuliao_stock_choose_photo)
    LinearLayout llPostFuliaoStockChoosePhoto;
    @Bind(R.id.tv_post_fuliao_stock_commit)
    TextView tvPostFuliaoStockCommit;
    private View view;

    private ArrayList<String> photos=new ArrayList<>();
    private File[] files;
    private String [] fileKeys;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_post_fuliao_stock, null);
        initView();
        return view;

    }

    @Override
    public void onEventMainThread(MainEvent event) {
        super.onEventMainThread(event);
        if (Utils.EVENTMSG.equals(event.msg)) {
            photos = event.photos;
            L.d(photos.get(0).toString());
            fileKeys = new String[photos.size()];
            files = new File[photos.size()];
            for (int i = 0; i < photos.size(); i++) {
                files[i] = new File(BitmapUtils.scal(photos.get(i)));
                fileKeys[i] = "photo" + (i + 1);
            }
        }

    }
    private void initView() {
        ButterKnife.bind(this, view);
        initEvent();
    }

    private void initEvent() {
        llPostFuliaoStockChoosePhoto.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.ll_post_fuliao_stock_choose_photo:
                Intent intent=new Intent(getActivity(),ChoosePhotosActivity.class);
                intent.putExtra("photos",photos);
                startActivity(intent);
                break;
            case R.id.tv_post_fuliao_stock_commit:

                if (files == null || files.length == 0) {
                    T.showShort(getActivity(), "请选择照片");
                } else {
                    String name = etPostFuliaoStockName.getText().toString();
                    String model=etPostFuliaoStockModel.getText().toString();
                    String format = etPostFuliaoStockFormat.getText().toString();
                    String brand = etPostFuliaoStockBrand.getText().toString();
                    String color = etPostFuliaoStockColor.getText().toString();
                    String num = etPostFuliaoStockNum.getText().toString();
                    String backup = etPostFuliaoStockBackup.getText().toString();
                    if (Utils.isEmpty(name)) {
                        T.showShort(getActivity(), "请输入辅料名称");
                    } else if (Utils.isEmpty(model)) {
                        T.showShort(getActivity(), "请输入辅料型号");
                    } else if (Utils.isEmpty(format)) {
                        T.showShort(getActivity(), "请输入辅料规格");
                    } else if (Utils.isEmpty(brand)) {
                        T.showShort(getActivity(), "请输入辅料品牌");
                    } else if (Utils.isEmpty(color)) {
                        T.showShort(getActivity(), "请输入颜色");
                    } else if (Utils.isEmpty(num)) {
                        T.showShort(getActivity(), "请输入数量");
                    } else if (Utils.isEmpty(backup)) {
                        T.showShort(getActivity(), "请输入其他说明");
                    } else {
                        Loading.show(getActivity(), getString(R.string.str_loading));
                        commit();
                    }
                }
                break;
        }
    }
    private void commit() {
        try {
            OkHttpClientManager.postAsyn(Url.BASEURL + Url.POST_FULIAO_STOCK, new OkHttpClientManager.ResultCallback<String>() {
                @Override
                public void onError(Request request, Exception e) {
                    Loading.dismiss();
                    T.showShort(getActivity(),e.toString());
                }

                @Override
                public void onResponse(String response) {
                    Loading.dismiss();
                    L.d(response);
                    T.showShort(getActivity(), response);
                }
            },files,fileKeys,getParams(etPostFuliaoStockName.getText().toString(),etPostFuliaoStockModel.getText().toString(),etPostFuliaoStockFormat.getText().toString(),
                    etPostFuliaoStockBrand.getText().toString(),etPostFuliaoStockColor.getText().toString(),
                    etPostFuliaoStockNum.getText().toString(),etPostFuliaoStockBackup.getText().toString()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public OkHttpClientManager.Param[] getParams(String title, String model, String format, String brand, String color, String num,
                                                 String backup){
        String timeline=Utils.getTimeline();
        String sign=Utils.md5(timeline);
        return new OkHttpClientManager.Param[]{
                new OkHttpClientManager.Param("timeline", timeline),
                new OkHttpClientManager.Param("sign", sign),
                new OkHttpClientManager.Param("userid", Utils.getUserId(getActivity(),"id")),
                new OkHttpClientManager.Param("title", title),
                new OkHttpClientManager.Param("model", model),
                new OkHttpClientManager.Param("format", format),
                new OkHttpClientManager.Param("brand", brand),
                new OkHttpClientManager.Param("color", color),
                new OkHttpClientManager.Param("num", num),
                new OkHttpClientManager.Param("backup", backup),
        };
    }
}
